package com.store.app.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.store.app.BaseActivity;
import com.store.app.MainActivity;
import com.store.app.R;
import com.store.app.adapter.PayoutMemberDTAdapter;
import com.store.app.bean.CouponUpdateBean;
import com.store.app.bean.MembersDTBean;
import com.store.app.net.NetRequest;
import com.store.app.net.ifc.IOnHttpListener;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by Administrator on 2017/2/15.
 * 派发
 */
public class PayoutActivity extends BaseActivity implements View.OnClickListener, IOnHttpListener {
    private TextView submit;
    private ImageView public_ll_return;
    private TextView tvTitle;
    private ListView lv_members;
    private NetRequest netRequest;
    private List<MembersDTBean> dtBeen = new ArrayList<>();
    private PayoutMemberDTAdapter memberDTAdapter;
    private CheckBox cb_all;
    private String item_id;
    private boolean isAll = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_payout);
        item_id = getIntent().getStringExtra("item_id");
        initView();
        initData();
    }

    private void initData() {
        netRequest = new NetRequest(this);
        netRequest.requestRegistMemeberList(1, MainActivity.member_id);
    }

    private void initView() {
        submit = (TextView) findViewById(R.id.submit);
        submit.setOnClickListener(this);
        public_ll_return = (ImageView) findViewById(R.id.public_ll_return);
        tvTitle = (TextView) findViewById(R.id.tvTitle);
        tvTitle.setText("派发");
        public_ll_return.setOnClickListener(this);
        lv_members = (ListView) findViewById(R.id.lv_members);
        cb_all = (CheckBox) findViewById(R.id.cb_all);
        memberDTAdapter = new PayoutMemberDTAdapter(dtBeen, this);
        lv_members.setAdapter(memberDTAdapter);
        cb_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               if(cb_all.isChecked()){
                   for (int i =0;i<dtBeen.size();i++){
                       dtBeen.get(i).setIsChoose(true);
                   }
               }else{
                   for (int i =0;i<dtBeen.size();i++){
                       dtBeen.get(i).setIsChoose(false);
                   }
               }
                memberDTAdapter.notifyDataSetChanged();
            }
        });

//        cb_all.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//                if(b){
//                    for(int i = 0;i<dtBeen.size();i++){
//                        dtBeen.get(i).setIsChoose(true);
//                    }
//                }else{
//                    for(int i = 0;i<dtBeen.size();i++){
//                        dtBeen.get(i).setIsChoose(false);
//                    }
//                }
//                memberDTAdapter.notifyDataSetChanged();
//            }
//        });
        lv_members.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Log.v("zyl", "onItemClick");
                dtBeen.get(i).setIsChoose(!dtBeen.get(i).isChoose());
                memberDTAdapter.notifyDataSetChanged();
                boolean temAll = true;
                for (int x = 0;x<dtBeen.size();x++){
                    if(!dtBeen.get(x).isChoose()){
                        temAll  = false;
                    }
                }
                if(temAll){
                    cb_all.setChecked(true);
                }else{
                    cb_all.setChecked(false);
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.public_ll_return:
                finish();
                break;
            case R.id.submit:
                String memberIdInfo = "";
                int num = 0;
                for (int i = 0;i<dtBeen.size();i++){
                    if(dtBeen.get(i).isChoose()){
                        num ++;
                        memberIdInfo+=dtBeen.get(i).consumer_id+",";
                    }
                }
                if(num>getIntent().getIntExtra("kucun",0)){
                        showToast("优惠券数量不足");
                        return;
                }
                if(TextUtils.isEmpty(memberIdInfo)){
                    showToast("请选择要派发的会员");
                    return;
                }
                netRequest.requestBatchHandoutCoupon(2,item_id,memberIdInfo);
                break;
        }
    }

    @Override
    public void onAfinalFail() {
        showToast("请检查本地网络");
    }

    @Override
    public void onExecuteSuccess(int requestId, String result, String msg) {
        if (requestId == 1) {
            try {
                JSONObject data = new JSONObject(result);
                Log.v("zyl", "data1:" + data.toString());
                Gson gson = new Gson();
                JSONArray listArr = data.getJSONArray("list");
                List<MembersDTBean> membersDTBeans = new ArrayList<>();
                for (int i = 0; i < listArr.length(); i++) {
                    MembersDTBean registDetailBean = gson.fromJson(listArr.getJSONObject(i).toString(), MembersDTBean.class);
                    membersDTBeans.add(registDetailBean);
                }
                dtBeen.addAll(membersDTBeans);
                memberDTAdapter.notifyDataSetChanged();
                Log.v("zyl","dtBeen size:"+dtBeen.size());

            } catch (Exception e) {
                e.printStackTrace();
            }
        }else if(requestId == 2){
            EventBus.getDefault().post(new CouponUpdateBean(1));
            showToast("派发完成");
            Intent intent = new Intent (this,CouponNewActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        }
    }

    @Override
    public void onExecuteFail(int requestId, String msg) {
        showToast(msg);
    }
}
